/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Gui;

import DataModel.*;

/**
 *
 * @author Rainiero
 */
public class ProjectGuiHelper {

    // <editor-fold defaultstate="collapsed" desc="Singleton">
    private static ProjectGuiHelper _instance = new ProjectGuiHelper();

    private ProjectGuiHelper()
    {
    }

    public static ProjectGuiHelper GetInstance()
    {
        return _instance;
    }
    // </editor-fold>

    public String PrintProjectsTable(ProjectSummary[] projects, int projectsTotalCount, int pageNumber, String address)
    {
        return PrintProjectsTable(projects, projectsTotalCount, pageNumber, address, false);
    }

    public String PrintProjectsTable(ProjectSummary[] projects, int projectsTotalCount, int pageNumber, String address, boolean addApprovalControls)
    {
        String html = "";
        if(addApprovalControls)
        {
            html += "<form action=\"../../srvRequestApproval\" method=\"post\" name=\"form1\" >";
            html += "<input type=\"hidden\" name=\"hdAccion\" id=\"hdAccion\">";
            html += "<input type=\"hidden\" name=\"hdNumero\" id=\"hdNumero\">";
        }
        html += "<table border=\"1\" align=\"center\">";
        html += "   <tr>";
        html += "       <th>País</th>";
        html += "       <th>Organización</th>";
        html += "       <th>Proyecto</th>";
        html += "       <th>Fecha inicial</th>";
        html += "       <th>Fecha final</th>";
        html += "       <th>Presupuesto</th>";
        html += "       <th>Detalles</th>";
        if(addApprovalControls)
        {
            html += "       <th>Aprobar</th>";
            html += "       <th>Rechazar</th>";
        }
        html += "   </tr>";

        for(int requestIndex = 0; requestIndex < projects.length; requestIndex++)
        {
            ProjectSummary project = projects[requestIndex];
            String style = "";
            if(requestIndex % 2 == 0)
                style = "alternateRow";
            html += "<tr>";
            html += String.format("<td class=\"%2$s\">%1$s</td>", project.getCountryName(), style);
            html += String.format("<td class=\"%2$s\">%1$s</td>", project.getOrganizationName(), style);
            html += String.format("<td class=\"%2$s\">%1$s</td>", project.getName(), style);
            html += String.format("<td class=\"%2$s\">%1$s</td>", project.getInitialDate(), style);
            html += String.format("<td class=\"%2$s\">%1$s</td>", project.getEndingDate(), style);
            html += String.format("<td class=\"%2$s\">%1$s</td>", GuiHelper.GetInstance().GetCurrencyText(project.getInitialBudget()), style);
            html += String.format("<td class=\"%2$s\"><a href=\"/SAFP-war/jsps/Common/ShowProject.jsp?project=%1$s\">Ver detalles</td>", project.getIdProyecto(), style);
            if(addApprovalControls)
            {
                html += String.format("<td class=\"%1$s\"><input type=\"submit\" value=\"Aprobar\" onclick=\"document.getElementById('hdNumero').value=%2$s;document.getElementById('hdAccion').value='aprobar'\"></td>", style, project.getIdProyecto());
                html += String.format("<td class=\"%1$s\"><input type=\"submit\" value=\"Rechazar\" onclick=\"document.getElementById('hdNumero').value=%2$s;document.getElementById('hdAccion').value='rechazar'\"></td>", style, project.getIdProyecto());
            }
            html += "</tr>";
        }

        int columns = 7;
        if(addApprovalControls)
            columns = 9;
        html += GuiHelper.GetInstance().GetPagination(projectsTotalCount, pageNumber, address, columns);
        html += "</table>";
        if(addApprovalControls)
            html += "</form>";
        return html;
    }

    public String PrintSchedule(ActivitySummary[] activities)
    {
        return PrintSchedule(activities, 0);
    }

    private String PrintSchedule(ActivitySummary[] activities, int level)
    {
        String result = "";
        GuiHelper guiHelper = GuiHelper.GetInstance();
        for(int activityIndex = 0; activityIndex < activities.length; activityIndex++)
        {
            ActivitySummary activity = activities[activityIndex];
            result += "<tr>";

            String spaces = "";
            for(int i = 0; i < level; i++)
                spaces += "&nbsp;&nbsp;";
            String style = "";
            if(activityIndex % 2 == 0)
                style = "alternateRow";
            result += String.format("<td class=\"%1$s\">%2$s<a href=\"ShowActivity.jsp?activity=%3$s\">%4$s</a></td>", style, spaces, activity.getId(), activity.getName());
            result += String.format("<td class=\"%1$s\">%2$s</td>", style, guiHelper.GetFormattedDate(activity.getEstimatedStart()));
            result += String.format("<td class=\"%1$s\">%2$s</td>", style, guiHelper.GetFormattedDate(activity.getEstimatedEnd()));
            result += String.format("<td class=\"%1$s\">%2$s</td>", style, guiHelper.GetCurrencyText(activity.getEstimatedBudget()));
            result += String.format("<td class=\"%1$s\">%2$s</td>", style, guiHelper.GetPercentageText(activity.getCompleted()));
            result += "</tr>";
            result += PrintSchedule(activity.getChildren(), level + 1);
        }
        return result;
    }

}
